<?php
//
// +------------------------------------------------------------------------+
// | Gecko Framework                                                        |
// +------------------------------------------------------------------------+
//

/**
 * @see Zend_Acl_Role_Interface
 */
require_once 'Zend/Acl/Role/Interface.php';

/**
 * Basic User class that implements basic security
 * when added to a Security Interface
 *
 * The basic data the user object needs is user_id,
 * user_name and real_name, if you want additional
 * data you have to extend this class
 *
 * @package Gecko;
 * @author Christopher Valderrama <valderrama.christopher@gmail.com>
 * @copyright Copyright (c) 2008
 * @version $Id$v1.0$ 2008
 * @access public
 **/
class Gecko_User implements Zend_Acl_Role_Interface {
	/**
	 * The user Id
	 *
	 * @var int
	 */
	protected $userID;
	/**
	 * The user name
	 * ej jhon
	 *
	 * @var string
	 */
	protected $userName;
	/**
	 * The user real name
	 * ej Jhon Doe
	 *
	 * @var unknown_type
	 */
	protected $realName;
	/**
	 * The security provider interface
	 *
	 * @var Gecko_Security_Interface
	 */
	protected $securityProvider = null;
	/**
	 * The user Roles this user belongs to
	 *
	 * @var array
	 */
	protected $userRoles = array();

	/**
	 * Creates a new instance of a Gecko_User
	 * It needs a stdClass object to populate
	 * the data
	 *
	 * @param object $data The data to create
	 */
	public function __construct($data) {
		$this->checkSettings(get_object_vars($data));
		$this->userID = (int) $data->user_id;
		$this->userName = $data->user_name;
		$this->realName = $data->real_name;
		$this->loadRoles();
	}

	/**
	 * Returns the user id
	 *
	 * @return string
	 */
	public function getUserId() {
		return $this->userID;
	}

	/**
	 * Returns the user name
	 *
	 * @return string
	 */
	public function getUserName() {
		return $this->userName;
	}

	/**
	 * Returns the user realname
	 *
	 * @return string
	 */
	public function getUserRealName() {
		return $this->realName;
	}

	/** Additional Functions **/

	/**
	 * Checks for required settings
	 *
	 * @param array $settings
	 */
	private function checkSettings($settings) {
		$error = "";

		if(empty($settings['user_id'])) {
			$error .= "user_id not defined";
		}

		if(empty($settings['user_name'])) {
			$error .= "user_name not defined";
		}

		if(empty($settings['real_name'])) {
			$error .= "real_name not defined";
		}

		if(!empty($error)) {
			throw new Gecko_Exception($error);
		}
	}

	/**
	 * Protected function for
	 * role autoloading on construction
	 *
	 * @return void
	 */
	protected function loadRoles() {
		$this->userRoles = array();
	}

	/**
	 * Sets the user roles this user is
	 * bound to
	 *
	 * @param array $roles
	 */
	public function setRoles($roles) {
		if( !is_array( $roles ) ) {
			throw new Gecko_Exception('$roles expected to be a array, ' . gettype( $roles ) . ' given');
		}

		$this->userRoles = $roles;
	}

	/**
	 * Returns the user Roles that are
	 * bound to this user
	 *
	 * @return array
	 */
	public function getRoles() {
		return $this->userRoles;
	}

	/**
	 * Check if a user belongs to a specific
	 * role
	 *
	 * @param unknown_type $role
	 * @return unknown
	 */
	public function hasRole($role) {
		foreach($this->userRoles as $_role) {
			if( $role === $_role ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Checks if the current user is allowed to
	 * perform a action, if no security provider
	 * is set, then the user is allowed to every
	 * action
	 *
	 * @param string $action
	 * @return boolean
	 */
	public function isAllowed($action = '', $resource = '') {
		if($this->securityProvider instanceof Gecko_Security_Interface) {
			if( !empty( $action ) ) {
				$this->securityProvider->setAction($action);
			}
			if(!empty( $resource )) {
				$this->securityProvider->setResource($resource);
			}

			return $this->securityProvider->isAllowed($this);
		} else {
			return true;
		}
	}

	/**
	 * Sets the security provider to check permissions
	 * when setting a security interface it will add itself
	 * so you must have the parentroles defined
	 *
	 * @param Gecko_Security_Interface $security The security object
	 */
	public function setSecurity(Gecko_Security_Interface $security ) {
		$this->securityProvider = $security;
		$this->securityProvider->getAcl()->addRole($this, $this->userRoles);
	}

	/**
	 * Implementing Zend_Acl_Role_Interface for custom
	 * permissions
	 *
	 * @return string
	 */
	public function getRoleId() {
		return $this->userName;
	}
}